package jobscheduling.algorithms.pso;

import java.util.Map;
import java.util.TreeMap;
import jobscheduling.algorithms.AbstractScheduler;
import jobscheduling.algorithms.ResultListener;
import jobscheduling.model.InputData;


public class PSOScheduler extends AbstractScheduler {
    private static final Map<String, String> _params = new TreeMap<String, String>();
    private static final Map<String, String> _descs = new TreeMap<String, String>();
    
    static {
        _params.put("iterations", "1000");
        _params.put("inertiaWeight", "1.0");
        _params.put("cognitionLearningFactor", "1.5");
        _params.put("socialLearningFactor", "1.5");
        _params.put("maxVelocity", "10.0");
        _params.put("particlesNumber", "40");
        
        _descs.put("iterations", "number of iterations");
        _descs.put("inertiaWeight", "inertia weight");
        _descs.put("cognitionLearningFactor", "cognition learning factor");
        _descs.put("socialLearningFactor", "social learning factor");
        _descs.put("maxVelocity", "maximum velocity of particle");
        _descs.put("particlesNumber", "number of particles");
    }
    
    public PSOScheduler (int[][] jobList, ResultListener listener, 
                         Map<String, String> params){
        super(jobList, listener, params);
    }
    
    public static Map<String, String> getParamsPresets() {
        return _params;
    }

    public static Map<String, String> getParamsDescs() {
        return _descs;
    }

    @Override
    public boolean isCorrect(String parameter, String value) {
        return true;
    }
    
    @Override
    public void run() {
    	int iterations = Integer.parseInt(_parameters.get("iterations"));
    	
    	int machinesNo = _jobs.length;
    	int jobsNo = _jobs[0].length;
    	int lczastek = Integer.parseInt(_parameters.get("particlesNumber"));
    	float inertiaWeight = Float.parseFloat(_parameters.get("inertiaWeight"));
    	float c1 = Float.parseFloat(_parameters.get("cognitionLearningFactor"));
    	float c2 = Float.parseFloat(_parameters.get("socialLearningFactor"));
    	float maxVelocity = Float.parseFloat(_parameters.get("maxVelocity"));
    	Evaluator evaluator = new Evaluator(new InputData(machinesNo, jobsNo, _jobs));

		Swarm s = new Swarm(_result, jobsNo, lczastek, evaluator, inertiaWeight, c1, c2, maxVelocity);
		s.seekSolution(iterations);
    }
}